<?php

namespace App\Http\Controllers\Warehousing;

use App\Http\Controllers\Controller;
use App\Http\Requests\Warehousing\Index\FileRequest;
use App\Http\Requests\Warehousing\Index\RemarkRequest;
use App\Http\Requests\Warehousing\Index\StoreRequest;
use App\Http\Requests\Warehousing\Index\UpdateRequest;
use App\Http\Searchs\Warehousing\IndexSearch;
use App\Repositories\Warehousing;
use Illuminate\Support\Facades\DB;

class IndexController extends Controller
{
    /**
     * @var Warehousing
     */
    private $warehousing;

    /**
     * IndexController constructor.
     * @param Warehousing $warehousing
     */
    public function __construct(Warehousing $warehousing)
    {
        $this->warehousing = $warehousing;
    }

    /**
     * Display a listing of the resource.
     *
     * @param IndexSearch $search
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(IndexSearch $search)
    {
        return $this->response(
            $this->warehousing->paginate($search->toArray())
        );
    }


    /**
     * Store a newly created resource in storage.
     *
     * @param StoreRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(StoreRequest $request)
    {
        return $this->response(
            DB::transaction(function () use ($request) {
                $this->warehousing->create($request->only([
                    'products', 'district', 'type', 'transport', 'packing_code', 'weight', 'piece', 'remark', 'customer_id'
                ]));
            })
        );
    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function show($id)
    {
        return $this->response(
            $this->warehousing->show($id)
        );
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function edit($id)
    {
        return $this->response(
            $this->warehousing->edit($id)
        );
    }

    /**
     * @param UpdateRequest $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function update(UpdateRequest $request, $id)
    {
        return $this->response(
            DB::transaction(/**
             * @author luffyzhao@vip.126.com
             */ function () use ($request, $id) {
                $this->warehousing->update($id, $request->only([
                    'products', 'district', 'type', 'transport', 'packing_code', 'weight', 'piece', 'remark', 'customer_id'
                ]));
            })
        );
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function confirm($id)
    {
        return $this->response(
            DB::transaction(/**
             * @author luffyzhao@vip.126.com
             */ function () use ($id) {
                $this->warehousing->confirm($id);
            })
        );
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function review($id)
    {
        return $this->response(
            DB::transaction(/**
             * @author luffyzhao@vip.126.com
             */ function () use ($id) {
                $this->warehousing->review($id);
            })
        );
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param RemarkRequest $request
     * @param int $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function reject(RemarkRequest $request, $id)
    {
        return $this->response(
            DB::transaction(/**
             * @author luffyzhao@vip.126.com
             */ function () use ($id, $request) {
                $this->warehousing->reject($id, $request->input('remark'));
            })
        );
    }

    /**
     * @param FileRequest $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function files(FileRequest $request, $id)
    {
        return $this->response(
            $this->warehousing->files($id, $request->only(['remark', 'file']))
        );
    }


    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     */
    public function windowTemplate($id){
        $this->warehousing->windowTemplate($id);
    }

    /**
     * @param $id
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     */
    public function windowBatch($id){
        $this->warehousing->windowBatch($id);
    }

    /**
     * @param $id
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     */
    public function windowTally($id){
        $this->warehousing->windowTally($id);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy($id)
    {
        return $this->response(
            DB::transaction(/**
             * @author luffyzhao@vip.126.com
             */ function () use ($id) {
                $this->warehousing->delete($id);
            })
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function arrival($id){
        return $this->response(
            DB::transaction(/**
             * @author luffyzhao@vip.126.com
             */ function () use ($id) {
                $this->warehousing->arrival($id);
            })
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function compete($id){
        return $this->response(
            DB::transaction(/**
             * @author luffyzhao@vip.126.com
             */ function () use ($id) {
                $this->warehousing->compete($id);
            })
        );
    }
}
